package utn.frsf.gabinete.fw.generator.html;

import java.util.ArrayList;

import utn.frsf.gabinete.fw.generator.Formulario;
import utn.frsf.gabinete.fw.generator.Input;


public class Form extends Html5Element {

	private Formulario form;
	
	public Form(Formulario form){
		this.hijos = new ArrayList<Html5Element>();
		this.form = form;
	}
	
	@Override
	public String abrirTag() {
		return "<form id=\""+form.getId()+"\">"+System.getProperty("line.separator")+
				"<fieldset>"+System.getProperty("line.separator")+
				"<legend>"+form.nombre() +"</legend>"+System.getProperty("line.separator");
	}

	@Override
	public String cerrarTag() {
		return "</fieldset>"+System.getProperty("line.separator")+ "</form>";
	}
	
	
	public void setFormToRender(Formulario form){
		this.form = form;
	}

	@Override
	public String generarElemento() {	
		cargarHijos();
		StringBuffer strBff = new StringBuffer();
		strBff.append(this.abrirTag());
		strBff.append("<ol>"+System.getProperty("line.separator"));
		for(Html5Element hijo : this.hijos){
			strBff.append("<li>"+System.getProperty("line.separator"));
			strBff.append(hijo.generarElemento());
			strBff.append("</li>"+System.getProperty("line.separator"));
		}
		strBff.append("</ol>"+System.getProperty("line.separator"));
		strBff.append("<input type=\"button\" id=\"guardar\" name=\"guardar\" value=\"Guardar\" class=\"ui-button ui-widget ui-state-default ui-corner-all ui-state-hover\" role=\"button\"  />"+System.getProperty("line.separator"));
		strBff.append("<input type=\"button\" id=\"borrar\" name=\"borrar\" value=\"Borrar\" class=\"ui-button ui-widget ui-state-default ui-corner-all ui-state-hover\" role=\"button\"  />"+System.getProperty("line.separator"));
		strBff.append("<input type=\"button\" id=\"nuevo\" name=\"nuevo\" value=\"Nuevo\" class=\"ui-button ui-widget ui-state-default ui-corner-all ui-state-hover\" role=\"button\"  />"+System.getProperty("line.separator"));

		strBff.append(this.cerrarTag());

		// agrego el ajax del form
		strBff.append("<script>"+System.getProperty("line.separator"));
		
//		strBff.append("$.fn.serializeObject = function() "+System.getProperty("line.separator")+
//				" {"+System.getProperty("line.separator")+
//				"   var o = {};"+System.getProperty("line.separator")+
//				"   var a = this.serializeArray();"+System.getProperty("line.separator")+
//				"   $.each(a, function() {"+System.getProperty("line.separator")+
//				"       if (o[this.name] !== undefined) {"+System.getProperty("line.separator")+
//				"           if (!o[this.name].push) {"+System.getProperty("line.separator")+
//				"               o[this.name] = [o[this.name]];"+System.getProperty("line.separator")+
//				"           }"+System.getProperty("line.separator")+
//				"           o[this.name].push(this.value || '');"+System.getProperty("line.separator")+
//				"       } else {"+System.getProperty("line.separator")+
//				"           o[this.name] = this.value || '';"+System.getProperty("line.separator")+
//				"       }"+System.getProperty("line.separator")+
//				"   });"+System.getProperty("line.separator")+
//				"   return o;"+System.getProperty("line.separator")+
//				" };");	
		
		
		strBff.append("$('#guardar').click(function() {"+System.getProperty("line.separator")+
		"	$.ajax({"+System.getProperty("line.separator")+
		"		url: \"/Html5fwControllerServlet\","+ System.getProperty("line.separator")+
		"		  type: \"POST\","+System.getProperty("line.separator")+
		"		  data: 'op=nuevo&ent="+this.form.getId()+"&datos='+JSON.stringify($('#"+this.form.getId()+"').serializeObject()),"+System.getProperty("line.separator")+
//		"		  dataType: 'json',  "+System.getProperty("line.separator")+
//		"		  contentType: 'application/json; charset=UTF-8',  "+System.getProperty("line.separator")+
		"		  success: function(data){ alert('success'); renderTabla(data) "+System.getProperty("line.separator")+
		"				  },"+System.getProperty("line.separator")+
//		"		  complete: function(data){ alert('complete'); renderTabla(data) "+System.getProperty("line.separator")+
//		"				  }"+System.getProperty("line.separator")+
		"		});"+System.getProperty("line.separator")+
		"} );"+System.getProperty("line.separator"));	
		
		strBff.append("</script>"+System.getProperty("line.separator"));
		strBff.append("<table id=\"tablaDetalle\" name=\"tablaDetalle\"><thead></thead><tbody></tbody></table>");
		return strBff.toString();
	}
	
	private void cargarHijos(){
		for(Input i : form.getElementos() ){
			this.hijos.add(i.getTipoElemento());
		}
	}
	
	
	
}
